//
//  AppDelegate.m
//  HiChat
//
//  Created by wqiong on 16/1/9.
//  Copyright © 2016年 wqiong. All rights reserved.
//

//#define APNSCert    @"TestHuanXin"          //环信推送证书名称

#import "AppDelegate.h"
#import "EaseMob.h"
#import "CQChatMainViewController.h"
#import "CQContactMainViewController.h"
#import "CQMineMainViewController.h"
#import "CQMineLoginViewController.h"

@interface AppDelegate ()<IChatManagerDelegate>

@property (nonatomic, retain) UITabBarController *tabBarC;

@end

@implementation AppDelegate

- (void)dealloc
{
    [_window release];
    [_tabBarC release];
    [super dealloc];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    [self creatTabBar];
    return YES;
}

- (void)creatTabBar
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    

    //判断rightBar是注销还是登录
    NSUserDefaults *useD = [NSUserDefaults standardUserDefaults];
    NSString *getName = [useD objectForKey:@"userName"];

    if (getName == nil) {
        
        CQMineLoginViewController *mineLoginVC = [CQMineLoginViewController shareMineLogInVC];
        
        self.window.rootViewController = mineLoginVC;
        
    } else {
        
        [self goInMain];
        
    }
    [_window release];
    
}

- (void)goInMain
{

    CQChatMainViewController *chatMainVC = [[[CQChatMainViewController alloc] init] autorelease];
    UINavigationController *chatMainNC = [[[UINavigationController alloc] initWithRootViewController:chatMainVC] autorelease];
    chatMainNC.navigationBar.translucent = NO;
    chatMainNC.tabBarItem = [[[UITabBarItem alloc] initWithTitle:@"会话" image:[UIImage imageNamed:@"09-chat2"] tag:10001] autorelease];
    
    CQContactMainViewController *contactMainVC = [[[CQContactMainViewController alloc] init] autorelease];
    UINavigationController *contactMainNC = [[[UINavigationController alloc] initWithRootViewController:contactMainVC] autorelease];
    contactMainNC.navigationBar.translucent = NO;
    contactMainNC.tabBarItem = [[[UITabBarItem alloc] initWithTitle:@"通讯录" image:[UIImage imageNamed:@"96-book"] tag:10002] autorelease];
    
    CQMineMainViewController *mineMainVC = [[[CQMineMainViewController alloc] init] autorelease];
    UINavigationController *mineMainNC = [[[UINavigationController alloc] initWithRootViewController:mineMainVC] autorelease];
    mineMainNC.navigationBar.translucent = NO;
    mineMainNC.tabBarItem = [[[UITabBarItem alloc] initWithTitle:@"我的" image:[UIImage imageNamed:@"53-house"] tag:10003] autorelease];

    
    NSUserDefaults *useD = [NSUserDefaults standardUserDefaults];
    NSString *getName = [useD objectForKey:@"userName"];
    NSString *getPassword = [useD objectForKey:@"userPassword"];
        //初始化环信SDK,apnsCertName为推送证书
        [[EaseMob sharedInstance] registerSDKWithAppKey:APPKEY apnsCertName:nil];
        //异步登陆的方法
        [[EaseMob sharedInstance].chatManager asyncLoginWithUsername:getName password:getPassword completion:^(NSDictionary *loginInfo, EMError *error) {
            //获取数据库中数据
            [[EaseMob sharedInstance].chatManager loadDataFromDatabase];
            
            [[EaseMob sharedInstance].chatManager addDelegate:self delegateQueue:nil];
            
            // 获取好友列表
            [[EaseMob sharedInstance].chatManager asyncFetchBuddyList];
            
            //
//            [[EaseMob sharedInstance] application:application
//                    didFinishLaunchingWithOptions:launchOptions];
            
        } onQueue:nil];

    self.tabBarC = [[UITabBarController alloc] init];
    self.tabBarC.tabBar.translucent = NO;
    self.tabBarC.tabBar.barTintColor = [UIColor orangeColor];
    self.tabBarC.viewControllers = [NSMutableArray arrayWithObjects:chatMainNC, contactMainNC, mineMainNC, nil];
    
    [self.tabBarC setSelectedIndex:1];
    self.window.rootViewController = self.tabBarC;

    [self.tabBarC release];

}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    // Saves changes in the application's managed object context before the application terminates.
}


@end
